package com.seio.edc.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.nova.edc.model.system.ModelConstant;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.nova.edc.model.system.DeviceType;
import com.nova.edc.model.system.DeviceLight;
import com.nova.edc.model.system.Login;
import com.nova.edc.service.system.DeviceTypeService;
import com.nova.edc.service.system.DeviceLightService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;

@Controller
@RequestMapping(value = "/systemDeviceLight")
public class DeviceLightContoller {
	@Autowired
	private DeviceLightService novaDeviceLightService;
	@Autowired
	private DeviceTypeService deviceTypeService;
	private final int PAGE_SIZE = 10;
	static Logger log = Logger.getLogger(DeviceLightContoller.class);

	@InitBinder("leEntity")
	public void initBinder1(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("leEntity.");
	}

	/**
	 * 灯具能耗表主页面
	 */
	@RequestMapping(value = "deviceLightList.shtml")
	public ModelAndView deviceLightList(HttpServletRequest request,
			HttpSession session, ModelMap modelMap, Integer pageIndex,
			DeviceLight lightEnergy) {
		// 所有灯具
		Page page = new Page();
		page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
		page.setPageSize(PAGE_SIZE);
		modelMap.put("pageIndex", page.getCurrentPage());
		novaDeviceLightService.trimLightEnergy(lightEnergy);
		PageList<DeviceLight> pageList = novaDeviceLightService
				.findPageLightEnergy(lightEnergy, page, "AddDate", "desc");
		modelMap.put("dataCount", page.getRowCount());
		modelMap.put("pageCount", page.getTotalPage());
		// 数据列表
		modelMap.put("list", pageList.getDatas());

		return new ModelAndView("system/deviceLightList", modelMap);
	}

	/**
	 * 添加初始化
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "deviceLightEdit.shtml")
	public ModelAndView deviceLightEdit(HttpServletRequest request,
			ModelMap modelMap, Integer id) {
		// 标记
		modelMap.put("editFlag", 0);
		if (id != null && id != 0) {
			DeviceLight lightEnergy = novaDeviceLightService.getById(id);
			modelMap.put("lightEnergy", lightEnergy);
			modelMap.put("editFlag", "update");
		} else {
			modelMap.put("editFlag", "add");
		}
		return new ModelAndView("system/deviceLightEdit", modelMap);
	}

	/**
	 * 添加login
	 */
	@RequestMapping(value = "save.shtml")
	public ModelAndView save(
			@ModelAttribute("deviceLight") DeviceLight deviceLight,
			HttpSession session, HttpServletRequest request, ModelMap modelMap) {
		Login sessionLogin = (Login) session.getAttribute("sessionLogin");
		DeviceType deviceType = deviceTypeService
				.getByName(ModelConstant.DEVICE_LIGHTENERGY);
		deviceLight
				.setDevicetypeid(deviceType != null ? deviceType.getId() : 0);
		if (deviceLight.getId() != null && deviceLight.getId() != 0) {
			deviceLight.setModifyby(sessionLogin.getUsername());
			deviceLight.setModifydate(DateUtils.getCurrentDate());
			deviceLight.setModifystamp(DateUtils.getCurrentDate());
			novaDeviceLightService.update(deviceLight);
		} else {
			deviceLight.setScrapped(0);
			deviceLight.setAdddate(DateUtils.getCurrentDate());
			deviceLight.setAddby(sessionLogin.getUsername());
			novaDeviceLightService.insert(deviceLight);
		}
		return deviceLightList(request, session, modelMap, null,
				new DeviceLight());

	}

	@RequestMapping(value = "delete.shtml")
	public ModelAndView delete(int id, HttpServletRequest request,
			HttpSession session, ModelMap modelMap) {
		novaDeviceLightService.deleteById(id);
		return deviceLightList(request, session, modelMap, null,
				new DeviceLight());
	}

	@RequestMapping(value = "checkLightcode.shtml")
	@ResponseBody
	public Map<String, Object> lightCodeVali(HttpServletRequest request,
			String lightCode, Integer lightType) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("existFlag",
				novaDeviceLightService.isExistCode(lightCode, lightType));
		return map;
	}
}
